/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import dao.Dao;
import entidades.Categoria;
import entidades.Post;
import entidades.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Maria Eloyza
 */
public class PostServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet PostServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet PostServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String op = request.getParameter("op");
        int id = Integer.parseInt(request.getParameter("id"));
        Dao<Post> daoPost = new Dao<Post>(Post.class);
        HttpSession session = request.getSession(true);
        if (op.equals("Ver")) {
            Post post = daoPost.get(id);
            session.setAttribute("objetoPost", post);
            response.sendRedirect("visualizarPost.jsp");
        }
        if (op.equals("Remover")) {
            daoPost.remove(id);
            List<Post> posts = daoPost.list();
            request.setAttribute("posts", posts);
            RequestDispatcher rd = request.getRequestDispatcher("post.jsp");
            rd.forward(request, response);

        }

        if (op.equals("Editar")) {
            Post post = daoPost.get(id);
            session.setAttribute("objetoPost", post);

            response.sendRedirect("editarPost.jsp");

        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String titulo= request.getParameter("tituloPost");
        int categoria = Integer.parseInt(request.getParameter("categoriaDoPost"));
        String texto = request.getParameter("textopost");
        String op = request.getParameter("op");
        Dao<Post> daoP = new Dao<Post>(Post.class);
        Dao<Categoria> daoC = new Dao<Categoria>(Categoria.class);

        if (op.equals("Buscar")) {
            
            if (titulo == null) {
                titulo = "";
            }            

            List<Post> posts = daoP.listByTituloAndCategoria(titulo,categoria);// adicionei no dao


            request.setAttribute("posts", posts);
            request.setAttribute("buscou", true);

        }
        
        if (op.equals("Inserir")) {
            
            Post p = new Post();
            p.setTitulo(titulo);
            p.setTexto(texto);
            p.setCategoria(daoC.get(categoria));
            HttpSession session = request.getSession(); 
            Usuario u = (Usuario)session.getAttribute("usuario");
            p.setUsuario(u); 
            p.setDataPost(new Date());
            daoP.insert(p);
            List<Post> posts = daoP.list();
            request.setAttribute("posts", posts);

        }
        
        if(op.equals("Editar")){
            HttpSession session = request.getSession(true);
            Post p = (Post) session.getAttribute("objetoPost");
            p.setTitulo(titulo);
            p.setCategoria(daoC.get(categoria));
            p.setTexto(texto);
            p.setTitulo(titulo);
            p.setDataPost(new Date());
           
            Usuario u = (Usuario) session.getAttribute("objetoUsuario");
            p.setUsuario(u); 
            daoP.update(p);
            List<Post> posts = daoP.list();
            session.removeAttribute("objetoPost");
            request.setAttribute("posts", posts);
        }

        RequestDispatcher rd = request.getRequestDispatcher("post.jsp");
        rd.forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
